documentWidth = window.screen.availWidth;
mainWidth = 0.92 * documentWidth;
cellSideLength = 0.18 * documentWidth;
cellSpace = 0.04 * documentWidth;

function getPosTop(i, j) {
    return cellSpace + (cellSpace + cellSideLength) * i;
}

function getPosLeft(i, j) {
    return cellSpace + (cellSpace + cellSideLength) * j;
}

function getNumberBackgroundColor(number) {
    switch (number) {
        case 2:
            return "#eee4da";
            break;
        case 4:
            return "#ede0c8";
            break;
        case 8:
            return "#f2b179";
            break;
        case 16:
            return "#f59563";
            break;
        case 32:
            return "#f67c5f";
            break;
        case 64:
            return "#f65e3b";
            break;
        case 128:
            return "#edcf72";
            break;
        case 256:
            return "#edcc61";
            break;
        case 512:
            return "#9c0";
            break;
        case 1024:
            return "#33b5e5";
            break;
        case 2048:
            return "#09c";
            break;
        case 4096:
            return "#a6c";
            break;
        case 8192:
            return "#93c";
            break;
        default:
            return "black"
    }
}

function getNumberColor(number) {
    if(number <= 4){
        return "#776e65"
    }
    return "white";
}

function noSpace(board) {
    console.log("come in noSpace()");
    for (var i = 0; i < 4; i++) {
        for (var j = 0; j < 4; j++) {
            if(board[i][j] == 0){
                console.log("board"+ i + "," + j+"still have space");
                // console.log();
                return false;
            }
        }
    }
    console.log("nospace here");
    return true;

}

function canMoveLeft(board) {
    for (var i = 0; i < 4; i++) {
        for (var j = 1; j < 4; j++) {
            if(board[i][j] != 0){
                if((board[i][j-1] == 0) || (board[i][j-1] == board[i][j])){
                    return true;
                }
            }
        }
    }
    return false;
}

function canMoveRight(board) {
    for (var i = 0; i < 4; i++) {
        for (var j = 2; j >= 0; j--) {
            if(board[i][j] != 0){
                if((board[i][j+1] == 0) || (board[i][j+1] == board[i][j])){
                    return true;
                }
            }
        }
    }
    return false;
}

function canMoveUp(board) {
    for (var j = 0; j < 4; j++) {
        for (var i = 1; i < 4; i++) {
            if(board[i][j] != 0){
                if((board[i-1][j] == 0) || (board[i-1][j] == board[i][j])){
                    return true;
                }
            }
        }
    }
    return false;
}

function canMoveDown(board) {
    for (var j = 0; j < 4; j++) {
        for (var i = 2; i >= 0; i--) {
            if(board[i][j] != 0){
                if((board[i+1][j] == 0) || (board[i+1][j] == board[i][j])){
                    return true;
                }
            }
        }
    }
    return false;
}

function noBlockX(row, col, col2, board) {
    for(var i = col + 1; i < col2; i++){
        if (board[row][i] != 0) {
            return false;
        }
    }
    return true;
}

function noBlockY(col, row, row2, board) {
    for(var i = row + 1; i < row2; i++){
        if (board[i][col] != 0) {
            return false;
        }
    }
    return true;
}

function noMove(board) {
    if(canMoveLeft(board)||canMoveRight(board)||canMoveDown(board)||canMoveUp(board)){
        console.log("noMove(): can move");
        return false;
    }else{
        console.log("noMove():can not move any more");
        return true;
    }
}
